1   /*
2    * Copyright (C) 2007 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect;
18  
19  import static java.util.Arrays.asList;
20  
21  import com.google.common.annotations.GwtCompatible;
22  import com.google.common.collect.testing.google.TestStringMultisetGenerator;
23  
24  import junit.framework.TestCase;
25  
26  import java.util.Arrays;
27  
28  /**
29   * Unit test for {@link HashMultiset}.
30   *
31   * @author Kevin Bourrillion
32   * @author Jared Levy
33   */
34  @GwtCompatible(emulated = true)
35  public class HashMultisetTest extends TestCase {
36  
37    private static TestStringMultisetGenerator hashMultisetGenerator() {
38      return new TestStringMultisetGenerator() {
39        @Override protected Multiset<String> create(String[] elements) {
40          return HashMultiset.create(asList(elements));
41        }
42      };
43    }
44  
45    public void testCreate() {
46      Multiset<String> multiset = HashMultiset.create();
47      multiset.add("foo", 2);
48      multiset.add("bar");
49      assertEquals(3, multiset.size());
50      assertEquals(2, multiset.count("foo"));
51    }
52  
53    public void testCreateWithSize() {
54      Multiset<String> multiset = HashMultiset.create(50);
55      multiset.add("foo", 2);
56      multiset.add("bar");
57      assertEquals(3, multiset.size());
58      assertEquals(2, multiset.count("foo"));
59    }
60  
61    public void testCreateFromIterable() {
62      Multiset<String> multiset
63          = HashMultiset.create(Arrays.asList("foo", "bar", "foo"));
64      assertEquals(3, multiset.size());
65      assertEquals(2, multiset.count("foo"));
66    }
67  
68    /*
69     * The behavior of toString() and iteration is tested by LinkedHashMultiset,
70     * which shares a lot of code with HashMultiset and has deterministic
71     * iteration order.
72     */
73  }
74